package com.bafa.objects.team;

import com.bafa.objects.Match;

import java.util.ArrayList;
import java.util.TreeMap;

public class TeamStats {
	
	private int biggestDefeat;
    private int biggestDefeatMatchId;
    
    private int biggestWin;
    private int biggestWinMatchId;
    private int cleanSheets;
    private int gamesDrawn;
    private int gamesLost;
    private int gamesWon;
    private int goalsConceded;
    private int goalsScored;
    private int mostConceded;
    private int mostConcededMatchId;
    private int mostScored;
    private int mostScoredMatchId;
    private TreeMap scorers = new TreeMap();
    private TreeMap<Integer,TeamSeasonStats> seasonStats = new TreeMap();
    private int teamId;
    public TeamStats() {
		// TODO Auto-generated constructor stub
	}
    public TeamStats(int teamId) {
        this.teamId=teamId;
    }
    
	public String[] scorers(){
		return (String[])scorers.values().toArray(new String[scorers.size()]);
	}
	public void addScorers(String name,String name1){
		scorers.put(name,name1);
	}
	public void addSeasonStat(int seasonId,TeamSeasonStats seasonStat){
		seasonStats.put(seasonId,seasonStat);
	}
    public void enterMatch(Match match,int seasonId){
		enterSeasonStat(seasonId);
        seasonStats.get(seasonId).addMatches(match.getID());
         
       
    }
    
    public void enterScorer(String name, int seasonId){
        if(!scorers.containsKey(name)){
            scorers.put(name,name);
        }
        
		seasonStats.get(seasonId).enterScorer(name);
        
    }
    
    public void enterSeasonStat(int seasonId){
        if(!seasonStats.containsKey(seasonId)){
            seasonStats.put(seasonId,new TeamSeasonStats(teamId));
        }
    }

    public int getBiggestDefeat() {
        return biggestDefeat;
    }

    public int getBiggestDefeatMatchId() {
        return biggestDefeatMatchId;
    }

    public int getBiggestWin() {
        return biggestWin;
    }

    public int getBiggestWinMatchId() {
        return biggestWinMatchId;
    }

    public int getCleanSheets() {
        return cleanSheets;
    }

    public int getGamesDrawn() {
        return gamesDrawn;
    }

    public int getGamesLost() {
        return gamesLost;
    }

    public int getGamesWon() {
        return gamesWon;
    }

    public int getGoalsConceded() {
        return goalsConceded;
    }

    public int getGoalsScored() {
        return goalsScored;
    }

    public int getMostConceded() {
        return mostConceded;
    }

    public int getMostConcededMatchId() {
        return mostConcededMatchId;
    }

    public int getMostScored() {
        return mostScored;
    }

    public int getMostScoredMatchId() {
        return mostScoredMatchId;
    }

    public TreeMap getScorers() {
        return scorers;
    }
    
    public TreeMap getSeasonStats() {
        return seasonStats;
    }

    public int getTeamId() {
        return teamId;
    }

    public void removeFixture(Match match){
		
		seasonStats.get(match.getSeasonId()).getMatches().remove(new Integer(match.getID()));
	}

    public void setBiggestDefeat(int biggestDefeat) {
        this.biggestDefeat = biggestDefeat;
    }

    public void setBiggestDefeatMatchId(int biggestDefeatMatchId) {
        this.biggestDefeatMatchId = biggestDefeatMatchId;
    }

    public void setBiggestWin(int biggestWin) {
        this.biggestWin = biggestWin;
    }

    public void setBiggestWinMatchId(int biggestWinMatchId) {
        this.biggestWinMatchId = biggestWinMatchId;
    }

    public void setCleanSheets(int cleanSheets) {
        this.cleanSheets = cleanSheets;
    }

    public void setGamesDrawn(int gamesDrawn) {
        this.gamesDrawn = gamesDrawn;
    }

    public void setGamesLost(int gamesLost) {
        this.gamesLost = gamesLost;
    }

    public void setGamesWon(int gamesWon) {
        this.gamesWon = gamesWon;
    }

    public void setGoalsConceded(int goalsConceded) {
        this.goalsConceded = goalsConceded;
    }

    public void setGoalsScored(int goalsScored) {
        this.goalsScored = goalsScored;
    }

    public void setMostConceded(int mostConceded) {
        this.mostConceded = mostConceded;
    }

    public void setMostConcededMatchId(int mostConcededMatchId) {
        this.mostConcededMatchId = mostConcededMatchId;
    }

    public void setMostScored(int mostScored) {
        this.mostScored = mostScored;
    }

    public void setMostScoredMatchId(int mostScoredMatchId) {
        this.mostScoredMatchId = mostScoredMatchId;
    }

    public void setScorers(TreeMap scorers) {
        this.scorers = scorers;
    }

    public void setSeasonStats(TreeMap seasonStats) {
        this.seasonStats = seasonStats;
    }
    
    public void setTeamId(int teamId) {
        this.teamId = teamId;
    }
	
	/**
     * 
     * Refresh the info
     * @param match
     */
    public void updateScores(Match match,int seasonId){
       if(match.isPlayed()){
       // FIrstly check to see home or away
        boolean home = (match.getTeamA()==teamId);
        
           if(home){
            // add total stats
               goalsScored += match.getGoalsA();
               goalsConceded += match.getGoalsB();
               if(match.getGoalsB()==0)cleanSheets+=1;
               if(match.getGoalsA()>match.getGoalsB()){
                   gamesWon +=1;
               }else if(match.getGoalsA()<match.getGoalsB()){
                   gamesLost +=1;
               }else{
                   gamesDrawn +=1;
               }
               
                   if(match.getGoalsA()-match.getGoalsB()>biggestWin){
                       biggestWinMatchId=match.getID();
                       biggestWin=match.getGoalsA()-match.getGoalsB();
                   }
                   if(match.getGoalsB()-match.getGoalsA()>biggestDefeat){
                       biggestDefeatMatchId=match.getID();
                       biggestDefeat=match.getGoalsB()-match.getGoalsA();
                   }
                   if(match.getGoalsA()>mostScored){
                        mostScoredMatchId=match.getID();
                       mostScored=match.getGoalsA();
                   }
                   if(match.getGoalsB()>mostConceded){
                        mostConcededMatchId=match.getID();
                       mostConceded=match.getGoalsB();
                   }
            }else{
                             
               goalsScored += match.getGoalsB();
               goalsConceded += match.getGoalsA();
               if(match.getGoalsA()==0)cleanSheets+=1;
               if(match.getGoalsB()>match.getGoalsA()){
                   gamesWon +=1;
               }else if(match.getGoalsB()<match.getGoalsA()){
                   gamesLost +=1;
               }else{
                   gamesDrawn +=1;
               }
               
                if(match.getGoalsB()-match.getGoalsA()>biggestWin){
                    biggestWinMatchId=match.getID();
                    biggestWin=match.getGoalsB()-match.getGoalsA();
                }
                if(match.getGoalsA()-match.getGoalsB()>biggestDefeat){
                    biggestDefeatMatchId=match.getID();
                    biggestDefeat=match.getGoalsA()-match.getGoalsB();
                }
                if(match.getGoalsB()>mostScored){
                     mostScoredMatchId=match.getID();
                    mostScored=match.getGoalsB();
                }
                if(match.getGoalsA()>mostConceded){
                     mostConcededMatchId=match.getID();
                    mostConceded=match.getGoalsA();
                }
            }
            
            seasonStats.get(seasonId).updateScores(match);
       }
    }
}
